package cloud.zhixinglu.www.spring.boot.mybatis.flex.service.impl;

import cloud.zhixinglu.www.spring.boot.mybatis.flex.entity.TestEntity;
import cloud.zhixinglu.www.spring.boot.mybatis.flex.mapper.TestMapper;
import cloud.zhixinglu.www.spring.boot.mybatis.flex.pojo.vo.TestVO;
import cloud.zhixinglu.www.spring.boot.mybatis.flex.service.TestService;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;

/**
 *  服务层实现。
 *
 * @author WeiXin
 * @since 2023-08-12
 */
@Service
public class TestServiceImpl extends ServiceImpl<TestMapper, TestEntity> implements TestService {

    @Resource
    private TestMapper mapper;

    /**
     * 获取 测试数据详情
     *
     * @param id String
     * @return TestEntity
     */
    @Override
    public TestEntity getTestByOperationId(String operationId) {

        QueryWrapper qw = QueryWrapper.create()
                .where(TestEntity::getOperationId).eq(operationId)
                .orderBy(TestEntity::getBusinessSend).asc()
                .limit(1);
        return mapper.selectOneByQuery(qw);
    }

    /**
     * 获取
     *
     * @param operationId {@link String} 操作ID
     * @return List<TestVO>
     */
    @Override
    public List<TestEntity> getTestListByOperationId(String operationId) {
        QueryWrapper qw = QueryWrapper.create()
                .where(TestEntity::getOperationId).eq(operationId)
                .orderBy(TestEntity::getBusinessSend).asc();
        return mapper.selectListByQuery(qw);
    }

    @Override
    public List<TestEntity> getTestAllList() {
        QueryWrapper qw = QueryWrapper.create()
                .orderBy(TestEntity::getBusinessSend).desc()
                .limit(100);
        return mapper.selectListByQuery(qw);
    }

    @Override
    public Page<TestEntity> pageTestListDesc(Page<TestEntity> page) {
        QueryWrapper qw = QueryWrapper.create()
                .orderBy(TestEntity::getBusinessSend).desc();
        return mapper.paginate(page, qw);
    }
}
